package com.javastudy.forum.controller;

import com.javastudy.forum.common.AppResult;
import com.javastudy.forum.common.ResultCode;
import com.javastudy.forum.config.AppConfig;
import com.javastudy.forum.model.User;
import com.javastudy.forum.service.IUserService;
import com.javastudy.forum.utils.MD5Utils;
import com.javastudy.forum.utils.StringUtils;
import com.javastudy.forum.utils.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author Chooker
 * @create 2023-08-16 18:29
 */
@RestController
@Slf4j
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController {
    @Resource
    IUserService userService;

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public AppResult register(@ApiParam("用户名") @RequestParam("username") @NonNull String username,
                              @ApiParam("昵称") @RequestParam("nickname") @NonNull String nickname,
                              @ApiParam("密码") @RequestParam("password") @NonNull String password,
                              @ApiParam("确定密码") @RequestParam("passwordRepeat") @NonNull String passwordRepeat) {
        if (!password.equals(passwordRepeat)) {
            return AppResult.failed(ResultCode.FAILED_TWO_PWD_NOT_SAME);
        }
        User user = new User();
        user.setUsername(username);
        user.setNickname(nickname);
        //对密码进行处理
        String salt = UUIDUtils.UUID_32();
        //对密码进行加密
        password = MD5Utils.md5Salt(password, salt);
        user.setPassword(password);
        user.setSalt(salt);
        userService.createNormalUser(user);
        return AppResult.success("注册成功");

    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public AppResult register(HttpServletRequest httpServletRequest,
                              @ApiParam("用户名") @RequestParam("username") @NonNull String username,
                              @ApiParam("密码") @RequestParam("password") @NonNull String password) {
        User user = userService.login(username, password);
        //1.获取session对象
        HttpSession session = httpServletRequest.getSession(true);
        //2.用户信息保存到session中
        session.setAttribute(AppConfig.SESSION_USER_KEY, user);

        return AppResult.success("登陆成功");

    }

    @ApiOperation("用户注销")
    @GetMapping("/logout")
    public AppResult logout(HttpServletRequest httpServletRequest) {
        //获取Session对象
        HttpSession session = httpServletRequest.getSession(false);
        if (session != null) {
            session.invalidate();
        }
        //返回结果
        return AppResult.success("注销成功");

    }

    @ApiOperation("获取用户信息")
    @GetMapping("/info")
    public AppResult<User> getInfo(HttpServletRequest httpServletRequest,
                                   @ApiParam("用户id") @RequestParam(value = "id", required = false) Long id) {
        User user;
        if (id == null) {
            //获取Session对象
            HttpSession session = httpServletRequest.getSession(false);
            user = (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        } else {
            user = userService.selectById(id);
        }

        return AppResult.success(user);
    }

    @ApiOperation("修改个人信息")
    @PostMapping("/modifyInfo")
    public AppResult<User> modifyInfo(HttpServletRequest httpServletRequest,
                                      @ApiParam("用户名(用于登录)") @RequestParam(value = "username", required = false) String username,
                                      @ApiParam("昵称") @RequestParam(value = "nickname", required = false) String nickname,
                                      @ApiParam("性别") @RequestParam(value = "gender", required = false) Byte gender,
                                      @ApiParam("邮箱") @RequestParam(value = "email", required = false) String email,
                                      @ApiParam("电话") @RequestParam(value = "phoneNum", required = false) String phoneNum,
                                      @ApiParam("个人简介") @RequestParam(value = "remark", required = false) String remark) {
        if (StringUtils.isEmpty(username) && StringUtils.isEmpty(nickname) && gender == null &&
                StringUtils.isEmpty(email) && StringUtils.isEmpty(phoneNum) && StringUtils.isEmpty(remark)) {
            return AppResult.failed("要修改的信息不能全为空");
        }
        HttpSession session = httpServletRequest.getSession(false);
        User user = (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        User updateUser = new User();
        updateUser.setId(user.getId());
        updateUser.setUsername(username); // 用户名
        updateUser.setNickname(nickname); // 昵称
        updateUser.setGender(gender); // 性别
        updateUser.setPhonenum(phoneNum); // 电话
        updateUser.setEmail(email); // 邮箱
        updateUser.setRemark(remark); // 个人简介

        userService.modifyInfo(updateUser);

        user = userService.selectById(user.getId());

        session.setAttribute(AppConfig.SESSION_USER_KEY, user);

        return AppResult.success(user);
    }

    @ApiOperation("修改密码")
    @PostMapping("/modifyPwd")
    public AppResult modifyPassword(@ApiParam("原密码") @RequestParam("oldPassword") @NonNull String oldPassword,
                                    @ApiParam("新密码") @RequestParam("newPassword") @NonNull String newPassword,
                                    @ApiParam("确认密码") @RequestParam("passwordRepeat") @NonNull String passwordRepeat,
                                    HttpServletRequest httpServletRequest) {
        //判断两次输入的密码
        if (!newPassword.equals(passwordRepeat)) {
            return AppResult.failed(ResultCode.FAILED_TWO_PWD_NOT_SAME);

        }
        //获取当前登录的用户信息
        HttpSession session = httpServletRequest.getSession(false);
        User user = (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        //修改密码
        userService.modifyPassword(user.getId(), newPassword, oldPassword);
        //销毁当前的session
        session.invalidate();
        return AppResult.success("修改成功");

    }


}
